home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / web / noweb / src / icon / totex.icn < prev    next >
Text File  |  1995-02-24  |  13KB  |  339 lines

  1. #line 4 "totex.nw"
  2. # Copyright 1991 by Norman Ramsey.  All rights reserved.
  3. # See file COPYRIGHT for more information.
  4. #line 10 "totex.nw"
  5. procedure main(args)
  6.   local delay
  7.   
  8. #line 38 "totex.nw"
  9. text := 1
  10. #line 116 "totex.nw"
  11. defns := table("")
  12. #line 250 "totex.nw"
  13. TeXspecials := '\\{}$&#^_ ~%'
  14. #line 13 "totex.nw"
  15.   delay := !args == "-delay"
  16.   noindex := !args == "-noindex"
  17.   while inputline := read() do inputline ? {
  18.     
  19. #line 69 "totex.nw"
  20. if ="@text " then       { text +:= *(line := tab(0))
  21.                           writes(if \quoting then TeXliteral(line)
  22.                                  else if \code then escape(line, '{}\\')
  23.                                  else line)                                      } else
  24. #line 74 "totex.nw"
  25. if ="@nl" & pos(0) then { if /code then {
  26. #line 89 "totex.nw"
  27. if text = 0 then writes("\\nwdocspar")
  28. text := 1
  29. #line 74 "totex.nw"
  30.                                                                                 }
  31.                           if \quoting then writes("\\nwnewline")
  32.                           write()                                                } else
  33. #line 60 "totex.nw"
  34. if ="@begin code " then { code := 1     ; writes("\\nwbegincode{", tab(0), "}")  } else
  35. if ="@end code "   then { code := &null ; writes("\\nwendcode{}")
  36.                           lastdefnlabel := &null                                 } else
  37. #line 96 "totex.nw"
  38. if ="@defn " then       { writes("\\sublabel{", \lastxreflabel, "}")
  39.                           writes("\\nwmargintag{", label2tag(\lastxreflabel), "}")
  40.                           writes("\\moddef{", convquotes(name := tab(0)), 
  41.                                  ("~" || label2tag(\lastxrefref)) | "",
  42.                                  "}\\", defns[name], "endmoddef")
  43.                           lastdefnlabel := lastxreflabel
  44.                           
  45. #line 154 "totex.nw"
  46. every lastxreflabel | lastxrefref := &null
  47. #line 103 "totex.nw"
  48.                           defns[name] := "plus"                                } else
  49. #line 64 "totex.nw"
  50. if ="@begin docs " then { if \delay & match(0) then 
  51. #line 84 "totex.nw"
  52. &null
  53. #line 64 "totex.nw"
  54.                                                                                    
  55.                           else {text := 0; writes("\\nwbegindocs{",tab(0),"}")}  } else
  56. if ="@end docs " then   { if \delay & match(0) then 
  57. #line 86 "totex.nw"
  58. { writes("\\nwfilename{", filename, "}"); delay := &null }
  59. #line 66 "totex.nw"
  60.                                                                                 
  61.                           else writes("\\nwenddocs{}")                           } else
  62. #line 105 "totex.nw"
  63. if ="@use " then        { writes("\\LA{}", convquotes(name := tab(0)), 
  64.                                  ("~" || label2tag(\lastxrefref)) | "",
  65.                                  "\\RA{}")                                     } else
  66. #line 140 "totex.nw"
  67. if ="@xref "               then { 
  68. #line 142 "totex.nw"
  69. if ="label "               then { lastxreflabel := tab(0)                         } else
  70. if ="ref "                 then { lastxrefref   := tab(0)                         } else
  71. if ="begindefs"   & pos(0) then { writes("\\nwalsodefined{")                      } else
  72. if ="defitem "             then { writes("\\\\{", tab(0), "}")                    } else
  73. if ="enddefs"     & pos(0) then { writes("}")                                     } else
  74. if ="beginuses"   & pos(0) then { writes("\\nwused{")                             } else
  75. if ="useitem "             then { writes("\\\\{", tab(0), "}")                    } else
  76. if ="enduses"     & pos(0) then { writes("}")                                     } else
  77. if ="notused "             then { writes("\\nwnotused{", TeXliteral(tab(0)), "}") } else
  78. if ="nextdef "             then {                                                 } else
  79. if ="prevdef "             then {                                                 } else
  80. #line 219 "totex.nw"
  81. if ="beginchunks" & pos(0) then {                                                 } else
  82. if ="chunkbegin "          then { label := tab(upto(' ')); =" "
  83.                                   writes("\\nwixlogsorted{c}{{", convquotes(tab(0)),
  84.                                       "}{", label, "}{")                          } else
  85. if ="chunkuse "            then { writes("\\nwixu{", tab(0), "}")                 } else
  86. if ="chunkdefn "           then { writes("\\nwixd{", tab(0), "}")                 } else
  87. if ="chunkend"    & pos(0) then { write("}}%")                                    } else
  88. if ="endchunks"   & pos(0) then {                                                 } else
  89. #line 140 "totex.nw"
  90.                                                   
  91. #line 156 "totex.nw"
  92. warn_unknown("xref " || tab(upto(' \t') | 0))
  93. #line 140 "totex.nw"
  94.                                                                                   } else
  95. #line 160 "totex.nw"
  96. if ="@index "               then { 
  97. #line 168 "totex.nw"
  98. if ="nl" & pos(0)          then        { write(if \code then "\\eatline" else "%")}else
  99. if =("defn "|"localdefn ") then 
  100. #line 172 "totex.nw"
  101. /noindex &
  102. #line 169 "totex.nw"
  103.                                        { 
  104. #line 181 "totex.nw"
  105. writes("\\nosublabel{", \lastxreflabel, "}")
  106. writes("\\nwindexdefn{", TeXliteral(name := tab(0)), "}{", indexlabel(name), "}{",
  107.         \lastxrefref, "}")
  108. #line 154 "totex.nw"
  109. every lastxreflabel | lastxrefref := &null
  110. #line 169 "totex.nw"
  111.                                                                                  } else
  112. if ="use "                 then 
  113. #line 172 "totex.nw"
  114. /noindex &
  115. #line 170 "totex.nw"
  116.                                        { 
  117. #line 189 "totex.nw"
  118. if /code then {
  119.   writes("\\protect\\nosublabel{", \lastxreflabel, "}")
  120.   writes("\\protect\\nwindexuse{", TeXliteral(name := tab(0)), "}{", 
  121.                 indexlabel(name), "}{", \lastxrefref, "}")
  122. }
  123. #line 154 "totex.nw"
  124. every lastxreflabel | lastxrefref := &null
  125. #line 170 "totex.nw"
  126.                                                                                  } else
  127. #line 199 "totex.nw"
  128. if ="begindefs" & pos(0) then 
  129. #line 172 "totex.nw"
  130. /noindex &
  131. #line 199 "totex.nw"
  132.                                      { writes("\\nwidentdefs{")                   } else
  133. if ="isused "            then 
  134. #line 172 "totex.nw"
  135. /noindex &
  136. #line 200 "totex.nw"
  137.                                      { "handled by latex"                         } else
  138. if ="defitem "           then 
  139. #line 172 "totex.nw"
  140. /noindex &
  141. #line 201 "totex.nw"
  142.                                      { i := tab(0); 
  143. #line 209 "totex.nw"
  144. writes("\\\\{{", TeXliteral(i), "}{", indexlabel(i), "}}") 
  145. #line 201 "totex.nw"
  146.                                                                                   } else
  147. if ="enddefs"   & pos(0) then 
  148. #line 172 "totex.nw"
  149. /noindex &
  150. #line 202 "totex.nw"
  151.                                      { writes("}")                                } else
  152. if ="beginuses" & pos(0) then 
  153. #line 172 "totex.nw"
  154. /noindex &
  155. #line 203 "totex.nw"
  156.                                      { writes("\\nwidentuses{"); ulist := []      } else
  157. if ="isdefined "         then 
  158. #line 172 "totex.nw"
  159. /noindex &
  160. #line 204 "totex.nw"
  161.                                      { "latex finds the definitions"              } else
  162. if ="useitem "           then 
  163. #line 172 "totex.nw"
  164. /noindex &
  165. #line 205 "totex.nw"
  166.                                      { i := tab(0); 
  167. #line 209 "totex.nw"
  168. writes("\\\\{{", TeXliteral(i), "}{", indexlabel(i), "}}") 
  169. #line 205 "totex.nw"
  170.                                                                                 
  171.                                         put(ulist, i);                            } else
  172. if ="enduses"   & pos(0) then 
  173. #line 172 "totex.nw"
  174. /noindex &
  175. #line 207 "totex.nw"
  176.                                      { writes("}"); 
  177. #line 211 "totex.nw"
  178. every i := !ulist do 
  179.   writes("\\nwindexuse{", TeXliteral(i), "}{", indexlabel(i), "}{", \lastdefnlabel, "}")
  180. #line 207 "totex.nw"
  181.                                                                                   } else
  182. #line 228 "totex.nw"
  183. if ="beginindex"  & pos(0) then 
  184. #line 172 "totex.nw"
  185. /noindex &
  186. #line 228 "totex.nw"
  187.                                        {                                          } else
  188. if ="entrybegin "          then 
  189. #line 172 "totex.nw"
  190. /noindex &
  191. #line 229 "totex.nw"
  192.                                        { label := tab(upto(' ')); =" "; name := tab(0)
  193.                                          write("\\nwixlogsorted{i}{{", TeXliteral(name),
  194.                                          "}{", indexlabel(name), "}}%")           } else
  195. if ="entryuse "            then 
  196. #line 172 "totex.nw"
  197. /noindex &
  198. #line 232 "totex.nw"
  199.                                        { "handled by latex"                       } else
  200. if ="entrydefn "           then 
  201. #line 172 "totex.nw"
  202. /noindex &
  203. #line 233 "totex.nw"
  204.                                        { "handled by latex"                       } else
  205. if ="entryend"    & pos(0) then 
  206. #line 172 "totex.nw"
  207. /noindex &
  208. #line 234 "totex.nw"
  209.                                        {                                          } else
  210. if ="endindex"    & pos(0) then 
  211. #line 172 "totex.nw"
  212. /noindex &
  213. #line 235 "totex.nw"
  214.                                        {                                          } else
  215. #line 160 "totex.nw"
  216.                                                     
  217. #line 162 "totex.nw"
  218. warn_unknown("index " || tab(upto(' \t') | 0))
  219. #line 160 "totex.nw"
  220.                                                                                   } else
  221. #line 120 "totex.nw"
  222. if ="@quote"    & pos(0)      then { quoting := 1     ; writes("{\\tt{}")          } else
  223. if ="@endquote" & pos(0)      then { quoting := &null ; writes("}")                } else
  224. if ="@file "                  then { filename := tab(0); 
  225. #line 154 "totex.nw"
  226. every lastxreflabel | lastxrefref := &null
  227. #line 123 "totex.nw"
  228.                                      \delay | writes("\\nwfilename{", filename, "}") } else
  229. if ="@literal "               then { writes(tab(0))                                } else
  230. if ="@header latex "          then { 
  231. #line 130 "totex.nw"
  232. writes("\\documentstyle[noweb]{article}\\pagestyle{noweb}\\noweboptions{", tab(0),
  233.        "}\\begin{document}")
  234. #line 125 "totex.nw"
  235.                                                                                    } else
  236. if ="@header tex "            then { writes("\\input nwmac ")                      } else
  237. if ="@trailer latex" & pos(0) then { write("\\end{document}")                      } else
  238. if ="@trailer tex"   & pos(0) then { write("\\bye")                                } else
  239. #line 53 "totex.nw"
  240. if ="@" then                                            # follows last else
  241.   warn_unknown(1(tab(upto(' ')|0), pos(0) | move(1)))
  242. else
  243.   write(&errout, "Botched line in noweb pipeline: ", tab(0))
  244. #line 17 "totex.nw"
  245.   }
  246.   write()
  247. end
  248. #line 109 "totex.nw"
  249. procedure label2tag(label)
  250.   return "{\\nwtagstyle{}\\subpageref{" || label || "}}"
  251. end
  252. #line 239 "totex.nw"
  253. procedure escape(line, chars, prefix)
  254.   /prefix := "\\"
  255.   line ? {
  256.     s := ""
  257.     while s ||:= tab(upto(chars)) do s ||:= prefix || move(1)
  258.     return s || tab(0)
  259.   }
  260. end
  261. #line 248 "totex.nw"
  262. global TeXspecials
  263. #line 257 "totex.nw"
  264. procedure TeXliteral(arg)
  265.   static nospace, code
  266.   initial { codes := ["\\", 92, "{", 123, "}", 125, "$", 36, "&", 38, "#", 35, "^", 94, 
  267.                       "_", 95, "%", 37, "~", 126]
  268.             code := table()
  269.             while (c := get(codes), n := get(codes)) do code[c] := string(n)
  270.             if c := !TeXspecials & c ~== " " & not member(code, c) then
  271.               stop("internal error, character-code mismatch, report a bug!")
  272.           }
  273.   s := ""
  274.   arg ? {
  275.     while s ||:= tab(upto(TeXspecials)) do {
  276.       c := move(1)
  277.       if member(code, c) then
  278.         s ||:= "{\\char" || code[c] || "}"
  279.       else
  280.         s ||:= "\\" || c
  281.     }
  282.     return s || tab(0)
  283.   }
  284. end
  285. #line 282 "totex.nw"
  286. procedure convquotes(s)
  287.   r := ""
  288.   s ? {
  289.     while r ||:= tab(find("[[")) do {
  290.       ="[[" | stop("impossible missing [[")
  291.       r ||:= "\\code{}" || TeXliteral(tab(find("]]")))
  292.       r ||:= tab(many(']')-2)
  293.       ="]]" | stop("impossible missing ]]")
  294.       r ||:= "\\edoc{}"
  295.     }
  296.     return r || tab(0)
  297.   }
  298. end
  299. #line 296 "totex.nw"
  300. procedure warn_unknown(tag)
  301.   static warned
  302.   initial warned := set()
  303.   if not member(warned, tag) then {
  304.     write(&errout, "Warning: unrecognized escape @", tag, tab(0))
  305.     insert(warned, tag)
  306.   }
  307.   return
  308. end
  309. #line 308 "totex.nw"
  310. procedure indexlabel(ident)
  311.   static badset, trans
  312.   initial {
  313.     
  314. #line 322 "totex.nw"
  315. trans := table()
  316. trans[" "] := "sp"      # space
  317. trans["#"] := "has"     # hash
  318. trans["$"] := "do"      # dollar
  319. trans["%"] := "pe"      # percent
  320. trans["&"] := "am"      # ampersand
  321. trans[","] := "com"     # commad
  322. trans[":"] := "col"     # colon
  323. trans["\\"] := "bs"      # backslash
  324. trans["^"] := "hat"     # hat
  325. trans["_"] := "un"      # underscore
  326. trans["{"] := "lb"      # left brace
  327. trans["}"] := "rb"      # right brace
  328. trans["~"] := "ti"      # tilde
  329. #line 312 "totex.nw"
  330.     badset := ''
  331.     every badset ++:= key(trans)
  332.   }
  333.   ident ? {
  334.     s := ""
  335.     while s ||:= tab(upto(badset)) do s ||:= ":" || trans[move(1)]
  336.     return s || tab(0)
  337.   }
  338. end
  339.